<?php
/**
 * 个人车主
 * @Author    紫梦
 * @DateTime  2018-02-01
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;
use \think\Request;
use app\common\model\Personal as PersonalModel;
use app\common\request\Oss;

class Personal extends Base
{
    
    public function index()
    {
        $model = PersonalModel::all();
        $ps_type = config('ps_type');

        $this->assign('data',$model);
        $this->assign('ps_type',$ps_type);
        return $this->fetch();
    }

    /**
     * 记录详情
     */
    public function detail($id = 1)
    {
        try {           
            $model = PersonalModel::get($id);                      
            
        } catch (\Exception $e) {
            
            return [
                'code'=>2,
                'msg'=>$e->getMessage(),
                ];
        }
        return $model;
    }

    /**
     * 添加
     */
    public function add()
    {
        $request = Request::instance();
        if($request->isAjax())
        {
            $data = $request->post();        
            $model = new PersonalModel();

            $file = $request->file('uploadfile');
            // 文件上传
            if($file)
            {
                $oss = new Oss();
                $data['uploadfile'] = $oss->uploadOss($file);
            }               
            $model->allowField(true)->save($data);
            $id = $model->id;
            if($id){
                return [
                    'code'=>1,
                    'id' => $id,
                    'msg'=>'添加成功'
                    ];
                
            }else{
                return [
                    'code'=>2,
                    'msg'=>'添加失败,请稍后再试'
                    ];
            }
        }else{

            return $this->fetch();
        }        
              
    }

    /**
     * 编辑记录
     */
    public function edit($id)
    {
        $request = Request::instance();


        if($request->isAjax())
        {
            $data = $request->post();
            $file = $request->file('uploadfile');
            $model = new PersonalModel();

            // 文件上传
            if($file)
            {
                $oss = new Oss();
                $data['uploadfile'] = $oss->uploadOss($file);
            }
            try {
                $model->allowField(true)->isUpdate(true)->save($data);
            } catch (\Exception $e) {
                return [
                    'code'=>3,
                    'msg'=>$e->getMessage(),
                    ];
            } 
            return [
                'code'=>1,
                'msg'=>'更新成功'
                ];
                    
        }else{

            $model = PersonalModel::get($id);

            $this->assign('data',$model);
            return $this->fetch();
        }
    }

    /**
     * 删除记录
     */
    public function delete($id)
    {
        try {
            PersonalModel::destroy($id);
            
        } catch (\Exception $e) {
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
                ];
            
        }
        
        return [
            'code'=>1,
            'msg'=>'删除成功'
            ];
    }

    public function status($id,$status)
    {
        try {
            PersonalModel::where('id', $id)
                        ->update(['status' => $status]);
            
        } catch (\Exception $e) {
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
                ];
            
        }
        
        return [
            'code'=>1,
            'msg'=>'更新成功'
            ];
    }

}